//
//  SecondViewController.m
//  TestDemo
//
//  Created by apple on 2022/1/15.
//

#import "SecondViewController.h"
#import "Student.h"
#import "Animal.h"
#import "ThreeViewController.h"

#import <objc/runtime.h>


@interface SecondViewController ()

@property(nonatomic,strong)Student * st;

@property(nonatomic,strong)Animal * ani;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"释放" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(releaseAction) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:btn];
    btn.frame =CGRectMake(100, 100, 50, 50);
    
    
    
    self.st = [Student instanceStudent];
    self.ani = [[Animal alloc]init];
    
    [self.ani addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];
    [self.st addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];
    

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

-(void)releaseAction{
    
    ThreeViewController * VC =[[ThreeViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
//    self.dog = nil;
//    NSLog(@"名字：%@",self.person.dog.name);
//    NSLog(@"释放后的对地址%p",self.dog);
//    NSLog(@"引用对象的地址%p",self.person.dog);
//    NSLog(@"引用对象的self.dog2地址%p",self.dog2);
//    [self testFunc2];
    
    
//    [self testFunc3:@"abcabcbb"];
//    [self testFunc4];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    
    self.st.name = @"李辉";
    self.ani.name = @"小黄";
    
}


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    
}

-(void)dealloc{
//    [self printIvarList:NSClassFromString(@"NSKVONotifying_Student")];
//    [self printPropertyList:NSClassFromString(@"NSKVONotifying_Student")];
//    [self printIvarList:NSClassFromString(@"Student")];
//    [self printPropertyList:NSClassFromString(@"Student")];
    [self printMethodList];
    [self.st removeObserver:self forKeyPath:@"name"];
    NSLog(@"%s",__func__);
    
}

-(void)printMethodList{
    unsigned int count;
    Method * methods = class_copyMethodList(NSClassFromString(@"NSKVONotifying_Student"), &count);
    for ( unsigned int i=0; i<count; i++) {
        Method method = methods[i];
        NSLog(@"%@---method_name---%@",,NSStringFromSelector(method_getName(method)));
    }
    
}

-(void)printPropertyList:(Class)class{
    
    unsigned int count;
    objc_property_t * propertys =  class_copyPropertyList(class, &count);
    for ( unsigned int i=0; i<count; i++) {
        objc_property_t property = propertys[i];
      const char * property_name =  property_getName(property);
        NSLog(@"%@----property---%@",NSStringFromClass(class),[NSString stringWithUTF8String:property_name]);
    }
    
}


-(void)printIvarList:(Class)class{
    unsigned int count;
    Ivar * ivars =  class_copyIvarList(class, &count);
    for ( unsigned int i=0; i<count; i++) {
       Ivar ivar = ivars[i];
      const char * ivar_name =  ivar_getName(ivar);
        NSLog(@"%@---ivar----%@",NSStringFromClass(class),[NSString stringWithUTF8String:ivar_name]);
    }
}


@end
